package com.topscore.uke.mvp.presenters;

import android.text.Editable;
import android.text.TextWatcher;

import com.topscore.uke.mvp.contractors.MemberBoundContractor;
import com.topscore.uke.mvp.model.MemberBoundModel;
import com.topscore.uke.utils.StringUtil;

public class MemberBoundPresenter implements MemberBoundContractor, MemberBoundContractor.Presenter {

    private View mView;

    private Model mModel;

    private String mPhoneNumber;
    private String mVerificationCode;

    public MemberBoundPresenter(View view) {
        this.mView = view;
        mModel = new MemberBoundModel(new Model.CallBack() {
            @Override
            public void onGetCodeSuccess() {
                mView.onGetVerifySuccess();
            }

            @Override
            public void onPostSuccess() {
                mView.onBindingSuccess();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mView.getPhoneEdt().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mPhoneNumber = editable.toString();
                setupPostBtn();
            }
        });
        mView.getVerificationEdt().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mVerificationCode = editable.toString();
                setupPostBtn();
            }
        });
    }

    @Override
    public void getVerificationCode() {
        if (!StringUtil.isEmpty(mPhoneNumber)){
            mView.onLoading("正在获取验证码");
            mModel.getVerify();
        }else {
            mView.onFail(-1,"请输入手机号码");
        }
    }

    @Override
    public void bindMember() {
        mView.onLoading("正在提交");
        mModel.bindMember();
    }

    private void setupPostBtn() {
        if (!StringUtil.isEmpty(mPhoneNumber) && !StringUtil.isEmpty(mVerificationCode)) {
            mView.setupButton(true);
        }else {
            mView.setupButton(false);
        }
    }
}
